package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地址管理实现类
 */
@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    //分页条件查询查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Address> page = addressDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Address> result = page.getResult();
        return new PageResult(total, result);
    }

    //删除
    @Override
    public void deleteById(Integer id) {
        addressDao.deleteById(id);
    }

    //根据id查询地址
    @Override
    public Address findById(Integer id) {
        return addressDao.findById(id);
    }

    //添加地址
    @Override
    public void add(Address address) {
        addressDao.add(address);
    }

    @Override
    public List<Address> findAll() {
        return addressDao.findAll();
    }

    @Override
    public List<Map> findAdd() {
        List<Map> list=new ArrayList<>();
        List<Address> all = addressDao.findAll();
        int num=1;
        for (Address address : all) {
            Map<String,String> map=new HashMap<>();
            map.put("value","选项"+num++);
            map.put("label",address.getAddress());
            list.add(map);
        }
        return list;
    }
}
